package com.dsp.cart.service.impl;

import com.dsp.cart.client.InventoryClient;
import com.dsp.cart.client.OrderClient;
import com.dsp.cart.client.dto.InventoryDTO;
import com.dsp.cart.client.dto.OrderDTO;
import com.dsp.cart.dao.CartDao;
import com.dsp.cart.dto.CartDTO;
import com.dsp.cart.entity.Cart;
import com.dsp.cart.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.HmilyTCC;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CartServiceImpl implements CartService {

    @Resource
    private CartDao cartDao;
    @Resource
    private InventoryClient inventoryClient;
    @Resource
    private OrderClient orderClient;

    public List<Cart> findAll() {
        return (List<Cart>) cartDao.findAll();
    }

    @HmilyTCC(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
    @Transactional(rollbackFor = Exception.class)
    public OrderDTO submitOrder(CartDTO cartDTO) {
        InventoryDTO inventoryDTO = new InventoryDTO();
        inventoryDTO.setId(cartDTO.getInventoryId());
        inventoryDTO.setNum(cartDTO.getNum());
        OrderDTO orderDTO =new OrderDTO();
        orderDTO.setName(cartDTO.getOrderName());

        OrderDTO order = orderClient.create(orderDTO);
        // if(Objects.isNull(order) || Objects.isNull(order.getId())){
        //     throw new HmilyRuntimeException("订单生成失败:"+orderDTO.getName());
        // }
        boolean flag = inventoryClient.decrease(inventoryDTO);
        // if(!flag){
        //     throw new HmilyRuntimeException("库存为扣减成功:"+inventoryDTO.getId());
        // }
        log.info("提交订单...成功,订单ID：{}，订单NAME:{}，库存ID:{}", orderDTO.getId(), orderDTO.getName(), cartDTO.getInventoryId());
        return order;
    }

    @HmilyTCC(confirmMethod = "confirmMethod", cancelMethod = "cancelMethod")
    @Transactional(rollbackFor = Exception.class)
    public OrderDTO submitOrderException(CartDTO cartDTO) {

        InventoryDTO inventoryDTO = new InventoryDTO();
        inventoryDTO.setId(cartDTO.getInventoryId());
        inventoryDTO.setNum(cartDTO.getNum());

        OrderDTO orderDTO =new OrderDTO();
        orderDTO.setName(cartDTO.getOrderName());

        OrderDTO order = orderClient.create(orderDTO);
        boolean flag = inventoryClient.decreaseException(inventoryDTO);
        log.info("提交订单...成功,订单ID：{}，订单NAME:{}，库存ID:{}", orderDTO.getId(), orderDTO.getName(), cartDTO.getInventoryId());
        return order;
    }

    @Transactional(rollbackFor = Exception.class)
    public OrderDTO confirmMethod(CartDTO cartDTO) {
        log.info("提交订单...确认,订单：{}，库存ID:{}，num:{}", cartDTO.getOrderName(), cartDTO.getInventoryId(), cartDTO.getNum());
        // final int decrease = inventoryDao.confirm(inventoryDTO.getId(),inventoryDTO.getNum());
        // if (decrease != 1) {
        //     throw new HmilyRuntimeException("库存确认异常:"+inventoryDTO.getId());
        // }
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setName(cartDTO.getOrderName());
        return orderDTO;
    }

    @Transactional(rollbackFor = Exception.class)
    public OrderDTO cancelMethod(CartDTO cartDTO) {
        log.info("提交订单...恢复,订单：{}，库存ID:{}，num:{}", cartDTO.getOrderName(), cartDTO.getInventoryId(), cartDTO.getNum());
        // final int decrease = inventoryDao.cancel(inventoryDTO.getId(),inventoryDTO.getNum());
        // if (decrease != 1) {
        //     throw new HmilyRuntimeException("库存恢复异常:"+inventoryDTO.getId());
        // }
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setName(cartDTO.getOrderName());
        return orderDTO;
    }
}
